﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.Adapters;
using System.Web.UI.WebControls;
using Eloje_Data;
using eLoje_Model;

namespace ELoje_web
{
    public partial class MedidaList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                gvMedida.PageIndex = 0;
                loadGrid();
                applyText();
                navigationHistory();
                
            }
        }

       protected void lnkAlterar_Click (object sender, CommandEventArgs e)
       {
           try
           {
               int codMedida = Convert.ToInt32(e.CommandArgument.ToString().Split(';')[0]);
               string descMedida = e.CommandArgument.ToString().Split(';')[1];
               string status = e.CommandArgument.ToString().Split(';')[2];

               if (Session["codMedida"] != null)
                   Session.Remove("codMedida");
               if (Session["descMedida"] != null)
                   Session.Remove("descMedida");
               if (Session["Status"] != null)
                   Session.Remove("Status");

               Session["codMedida"] = codMedida;
               Session["descMedida"] = descMedida;
               Session["Status"] = status;

               Response.Redirect("~/Cadastros/MedidaNew.aspx");
           }
           catch (Exception)
           {
               
               throw;
           }
       }

       protected void lnkLog_Click(object sender, CommandEventArgs e)
       {
           try
           {
               int codMedida = Convert.ToInt32(e.CommandArgument.ToString());
               

               if (Session["codMedida"] != null)
                   Session.Remove("codMedida");
               
               Session["codMedida"] = codMedida;
               
               Server.Transfer("~/Logs/MedidaLog.aspx");
           }
           catch (System.Threading.ThreadAbortException)
           { }
           catch (Exception)
           {

               throw;
           }
       }

       protected void gvMedida_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType .Equals(DataControlRowType.DataRow))
           {
               if(((MedidaVO)e.Row.DataItem).STATUS.Equals("S"))
               {
                   ((LinkButton)e.Row.Cells[3].FindControl("lnkEnableDisable")).Text = "Desabilitar";
               }

               if(((MedidaVO)e.Row.DataItem).STATUS.Equals("N"))
               {
                   ((LinkButton)e.Row.Cells[3].FindControl("lnkEnableDisable")).Text = "Habilitar";
               }
           }

       }

       protected void gvMedida_PageIndexChanging(object sender, GridViewPageEventArgs e)
       {
           gvMedida.PageIndex = e.NewPageIndex;
           loadGrid();
       }

       protected void gvMedida_RowCreated(object sender, GridViewRowEventArgs e)
       {
            if(e.Row.RowType.Equals(DataControlRowType.DataRow))
            {
                LinkButton a = (LinkButton)e.Row.FindControl("lnkAlterar");
                ScriptManager.GetCurrent(this).RegisterPostBackControl(a);
                LinkButton d = (LinkButton)e.Row.FindControl("lnkEnableDisable");
                ScriptManager.GetCurrent(this).RegisterPostBackControl(d);
                LinkButton l = (LinkButton)e.Row.FindControl("lnkLog");
                ScriptManager.GetCurrent(this).RegisterPostBackControl(l);

            }
       }

        protected void btnAddNewMedida_Click (object sender, EventArgs e)
       {
           Response.Redirect("~/Cadastros/MedidaNew.aspx");
       }
        #region Métodos
        private void loadGrid()
        {
            try
            {
                MedidaDA da = new MedidaDA();
                gvMedida.DataSource = da.getMedida();
                gvMedida.DataBind();
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void applyText()
        {
            btnAddNewMedida.Text = "Nova Medida";
        }

        private void navigationHistory()
        {
            lblHistoryNavigation.Text = String.Format("{0} >> {1} >> ", "Cockpit", "Cadastros");
            lblThisPage.Text = "Medidas"; 

        }
        #endregion

        protected void lnkEnableDisable_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int codMedida = Convert.ToInt32(e.CommandArgument.ToString().Split(';')[0]);
                string status = e.CommandArgument.ToString().Split(';')[1];
                string descMedida = e.CommandArgument.ToString().Split(';')[2];

                MedidaDA da = new MedidaDA();

                if (status.ToUpper().Equals("S"))
                    da.setDisableMedida(codMedida, descMedida, "Desenv");
                else
                    da.setEnableMedida(codMedida, descMedida, "Desenv");

                loadGrid();
            }
            catch (Exception)
            {

                throw;
            }
        }
                             
    }
}